package com.kailu.hotel.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.kailu.hotel.model.ServiceModel;
import com.kailu.hotel.service.ServiceManager;

/**
 * @author viet.ch
 * @generated Apr 22, 2014
 * @from VN
 */

@Controller
public class ServiceController {
	
	 	@Autowired
	    private ServiceManager serviceManager;
	 
	    @RequestMapping(value = "/servicemanager", method = RequestMethod.GET)
	    public String listService(ModelMap map)
	    {
	        map.addAttribute("serviceList", serviceManager.getAllService());
	        return "servicemanager";
	    }
	 
	    @RequestMapping(value = "/servicemanager/add", method = RequestMethod.POST)
	    public String addService(@ModelAttribute(value="service") ServiceModel service, BindingResult result)
	    {
	        serviceManager.addService(service);
	        return "redirect:/servicemanager";
	    }
	    
	    @RequestMapping("/servicemanager/addnew")
	    public String add(ModelMap map)
	    {
	        map.addAttribute("service", new ServiceModel());
	        return "servicemanager_add";
	    }
	 
	    @RequestMapping("/servicemanager/delete/{service_id}")
	    public String deleteService(@PathVariable("service_id") int service_id)
	    {
	        serviceManager.deleteService(service_id);
	        return "redirect:/servicemanager";
	    }
	 
	    @RequestMapping(value = "/servicemanager/update/{service_id}", method = RequestMethod.GET)
	    public String getService(@PathVariable("service_id") int service_id, ModelMap map) {
	    	map.addAttribute("service", serviceManager.getServiceById(service_id));
	    	return "updateService";
	    }
	    
	    @RequestMapping(value = "/servicemanager/update/takeupdate", method = RequestMethod.POST)
	    public String updateService(@ModelAttribute(value="service") ServiceModel service, BindingResult result)
	    {
	        serviceManager.updateService(service);
	        return "redirect:/servicemanager";
	    }
	    
	    
	    @RequestMapping(value = "/servicemanager", method = RequestMethod.POST)
	    public String searchService(@ModelAttribute(value="search") String search, ModelMap map)
	    {
	    	if (!search.isEmpty()) {
	    		map.addAttribute("serviceList",	serviceManager.searchService(search));
	    	} else {
	    		 map.addAttribute("serviceList", serviceManager.getAllService());
	    	}
	        return "servicemanager";
	    }

}
